package org.eclipse.ea.requisit.manager.popup.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.ea.requisit.manager.utils.RequirementCostsComparator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class ResultCostsTable {
	
	private Table table;
	private TableEditor tableEditor;
	private List<String> requirements;
	private List<Double> values;
	
	public ResultCostsTable() {
		
	}
	
	public void setColumnNames(){
		
		TableColumn column = null;
		
		//Coluna dos requisitos
		column = new TableColumn(table, SWT.NONE);
		column.setWidth(70);
		column.setText("Requisito");
		
		//Coluna dos resultados
		column = new TableColumn(table, SWT.NONE);
		column.setWidth(50);
		column.setText("%");
	}
	
	public void reloadData(){
		
		List<RequirementCost> requirementCosts = orderValues(); 
		
		for (RequirementCost requirementCost : requirementCosts) {
			
			String[] data = new String[]{requirementCost.getRequirement(), 
					                     String.valueOf(requirementCost.getCost())};
			
			TableItem item = new TableItem (table, SWT.NONE);
			item.setText(data);
		}
			
		this.table.setSelection(0);
	}
	
	private List<RequirementCost> orderValues(){
		
		List<RequirementCost> requirementCosts = new ArrayList<RequirementCost>();
		
		// cria a lista
		for(int i = 0; i < requirements.size(); i++){
			
			RequirementCost requirementCost = new RequirementCost(requirements.get(i), values.get(i));
			requirementCosts.add(requirementCost);
		}
		
		//ordena do maior valor para o menor
		Collections.sort(requirementCosts, new RequirementCostsComparator());
		
		return requirementCosts;
	}

	public Table getTable() {
		return table;
	}

	public void setTable(Table table) {
		this.table = table;
	}

	public TableEditor getTableEditor() {
		return tableEditor;
	}

	public void setTableEditor(TableEditor tableEditor) {
		this.tableEditor = tableEditor;
	}

	public List<String> getRequirements() {
		return requirements;
	}

	public void setRequirements(List<String> requirements) {
		this.requirements = requirements;
	}

	public List<Double> getValores() {
		return values;
	}

	public void setValores(List<Double> valores) {
		this.values = valores;
	}
}
